package com.bwf.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/26 10:45
 * @Copyright 博为峰
 */
public class UDPReceDemo {
    public static void main(String[] args) throws Exception {
        /*
         创建UDP传输接收端
          1.创建udp的socket服务对象
          2.创建DatagramPacket数据报包对象，封装要接收的数据
          3.接收数据包
          4.关闭socket服务
         */
        DatagramSocket ds = new DatagramSocket(9999);
        byte[] bytes = new byte[1024];
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
        ds.receive(packet);
        String rece = new String(packet.getData(), 0, packet.getLength());
        System.out.println(rece);
        System.out.println(packet.getAddress().getHostName());
        ds.close();
    }
}
